﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace ProductStore.Models
{
    public class EntityRepository<T> : IEntityRepository<T>
        where T : class, new()
    {
        private readonly ProductStoreEntities _entities;

        public EntityRepository() : this(new ProductStoreEntities()) { }

        public EntityRepository(ProductStoreEntities entities)
        {
            _entities = entities;
        }

        public void CommitChanges()
        {
            _entities.SaveChanges();
        }

        public void DeleteOnCommit(T entity)
        {
            _entities.Set<T>()
                .Remove(entity);
        }

        public T Get(int key)
        {
            return _entities.Set<T>().Find(key);
        }

        public IQueryable<T> GetAll()
        {
            return _entities.Set<T>();
        }

        public void InsertOnCommit(T entity)
        {
            _entities.Set<T>()
                .Add(entity);
        }
    }
}